// 判断类型
export const _type = (o) => {
  if (o === null) return 'null'
  if (o === undefined) return 'undefined' //兼容ie8
  var s = Object.prototype.toString.call(o)
  var t = s.match(/\[object\s(\w*?)\]/)[1].toLowerCase()
  return t === 'number' ? (isNaN(o) ? 'nan' : !isFinite(o) ? 'infinity' : t) : t
}

export const isUndefined = o => {
  return _type(o) === 'undefined'
}
export const isObject = o => {
  return _type(o) === 'object'
}


//首字母大写 _capitalize
export const _capitalize = (t) => {
  return t.slice(0, 1).toUpperCase() + t.slice(1)
}
//小驼峰
export const _camelCase = (arr) => {
  return arr.map((t, index) => index === 0 ? t : _capitalize(t)).join("")
}


export const _urlParams = (str = location.href, separator = '&') => {
  let query = String(str).match(/^(?:[^?]*\?)?([\w\d\-\/=&%]+)/),
    key,
    value;
  if (query == null) return hash;
  query = query.pop();
  return query.split(separator).reduce((hash, pair) => {
    if (pair.indexOf('=') > 0) {
      pair = decodeURIComponent(pair).split('=');

      key = pair.shift();
      // 如果query中某个变量值包含等号
      // 我们应该重新组合起来
      value = pair.join('=');

      if (value != void 0) {
        value = value.replace('+', ' ');
      }
    } else {
      key = decodeURIComponent(pair);
      value = void 0;
    }

    hash[key] = value;
    return hash;
  }, {});
}





// 判断属性
export const _has = (obj = {}, property) => {
  return obj.hasOwnProperty(property)
}
export const deepClone = (obj) => {
  return JSON.parse(JSON.stringify(obj))
}


// 修改属性名
export const changeProperty = (obj, from, to) => {
  let target = deepClone(obj)
  if (!_has(target, to)) {
    target[to] = target[from]
    deleteProperty(target, from)
  }
  return target
}
// 删除属性
export const deleteProperty = (obj, property) => {
  delete obj[property]
  return obj
}


// foreach增加一个next参数
export const eachNext = (points, callback) => {
  let n = points.length
  points.forEach((t, index) => {
    let next = points[index + 1 >= n ? 0 : index + 1]
    callback && callback(t, index, next)
  })
}


export const plainTreeMenu = (tree) => {
  let list = []
  const fn = (t) => {
    t.forEach(t => {
      if (t.children) {
        fn(t.children)
      } else {
        list.push(t)
      }
    })
  }
  fn(tree)
  return list
}


// 分割符
export const propsSplitChar = "_"
export const getValue = (obj = {}, prop) => {
  let index = prop.indexOf(propsSplitChar)
  if (index === -1) {
    return obj[prop]
  } else {
    return getValue(obj[prop.slice(0, index)], prop.slice(index + propsSplitChar.length))
  }
}
export const setValue = (obj, prop, value) => {
  let index = prop.indexOf(propsSplitChar)
  if (index === -1) {
    obj[prop] = value
  } else {
    if (!obj[prop.slice(0, index)]) {
      obj[prop.slice(0, index)] = {}
    }
    setValue(obj[prop.slice(0, index)], prop.slice(index + propsSplitChar.length), value)
  }
}
// 屏幕大小
export const _screen = () => {
  // let width = window.innerWidth, height = window.innerHeight
  let width = 800, height = 600
  return {
    width,
    height,
    o: [width / 2, height / 2]
  }
}